﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using SDLITS.Common;
using SDLITS.Model;
using FineUI;
using MySoft.Data;


namespace SDLITS.Web.SystemManage
{
    public partial class SystemUserIF : CommonBase
    {
        List<Department> areall = SDDbSession.Default.From<Department>().ToList();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTree();
                Region3.IFrameUrl = "SystemUserManage.aspx?aid=";
            }
        }

        private void BindTree()
        {
            List<Department> tops = areall.FindAll(p => p.DirID == 0);
            foreach (Department row in tops)
            {
                int childs = areall.FindAll(p => p.DirID == row.DeptID).Count;
                int count = SDDbSession.Default.From<User>().Where(SDLITS.Model.User._.DeptID == row.DeptID).Count();
                TreeNode node = new TreeNode();
                node.Text = row.DeptName + "(" + count + ")";
                node.Target = "main";
                node.NavigateUrl = "SystemUserManage.aspx?aid=" + row.DeptID;
                treeMenu.Nodes.Add(node);
                if (childs > 0)
                {
                    ResolveSubTree(row.DeptID, node);
                }
            }                       
            TreeNode n1 = new TreeNode();
            n1.Text = "全部系统用户";
            n1.Target = "main";
            n1.NavigateUrl = "SystemUserManage.aspx?aid=";
            treeMenu.Nodes.Insert(0, n1);
        }
        private void ResolveSubTree(int id, TreeNode treeNode)
        {
            List<Department> lists = areall.FindAll(p => p.DirID == id);
            if (lists.Count > 0)
            {
                foreach (Department row in lists)
                {
                    int count = SDDbSession.Default.From<User>().Where(SDLITS.Model.User._.DeptID == row.DeptID).Count();
                    TreeNode node = new TreeNode();
                    node.Text = row.DeptName + "(" + count + ")";
                    node.Target = "main";
                    node.NavigateUrl = "SystemUserManage.aspx?aid=" + row.DeptID;
                    treeNode.Nodes.Add(node);
                    treeNode.Expanded = true;
                    int childs = areall.FindAll(p => p.DirID == row.DeptID).Count;
                    if (childs>0)
                    {
                        ResolveSubTree(row.DeptID, node);
                    }
                }
            }
        }
    }
}